package com.ygqh.baby.utils;

import com.foxinmy.weixin4j.util.StringUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * @version V1.0
 * @ClassName: ArraysUtil
 * @author: (guohao)
 * @date: 2018/7/23 15:41
 */
public class LocalArraysUtil {

    public static <T> List<T> convertToList(T... arr) {
        List<T> list = new ArrayList<>(Arrays.asList(arr));
        return list;

    }

    public static <T> void removeArrRepet(Long... arr) {

        if (arr != null) {
            TreeSet<Long> hset = new TreeSet<Long>(Arrays.asList(arr));
            arr = hset.toArray(new Long[hset.size()]);
        }
    }

    public static <T> void removeListRepet(List<Long> list) {

        if (list != null) {
            TreeSet<Long> hset = new TreeSet<Long>(list);
            list.clear();
            list.addAll(hset);
        }
    }

    public static <T> void removeRepet(List<T> list1, List<T> list2) {

        if (CollectionUtils.isEmpty(list2) || CollectionUtils.isEmpty(list1)) {
            return;
        }
        Iterator<T> iterator = list1.iterator();
        while (iterator.hasNext()) {
            T next = iterator.next();
            if (list2.contains(next)) {
                iterator.remove();
                list2.remove(next);
            }
        }
    }

    public static <T> Boolean equals(T[] arr, List<T> list) {

        if (arr == null || CollectionUtils.isEmpty(list)) {
            return false;
        }
        List<T> ts = convertToList(arr);
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T next = iterator.next();
            if (ts.contains(next)) {
                iterator.remove();
                ts.remove(next);
            }
        }
        return ts.size() == 0 && list.size() == 0;
    }

    public static <T> String getString(T[] arr) {
        if (arr == null) {
            return "";
        }
        Arrays.sort(arr);
        return StringUtil.join(arr, ",".charAt(0));
    }
}
